<?php


abstract class BaseUser extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $username;


	
	protected $password;


	
	protected $phone;


	
	protected $gender = 0;


	
	protected $avatar_path;


	
	protected $total_ringtone = 0;


	
	protected $phone_model;


	
	protected $phone_maker;


	
	protected $ringtone_id;


	
	protected $is_online = 0;


	
	protected $session_expire;


	
	protected $created_datetime;


	
	protected $update_datetime;


	
	protected $enabled;

	
	protected $collRingtones;

	
	protected $lastRingtoneCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getUsername()
	{

		return $this->username;
	}

	
	public function getPassword()
	{

		return $this->password;
	}

	
	public function getPhone()
	{

		return $this->phone;
	}

	
	public function getGender()
	{

		return $this->gender;
	}

	
	public function getAvatarPath()
	{

		return $this->avatar_path;
	}

	
	public function getTotalRingtone()
	{

		return $this->total_ringtone;
	}

	
	public function getPhoneModel()
	{

		return $this->phone_model;
	}

	
	public function getPhoneMaker()
	{

		return $this->phone_maker;
	}

	
	public function getRingtoneId()
	{

		return $this->ringtone_id;
	}

	
	public function getIsOnline()
	{

		return $this->is_online;
	}

	
	public function getSessionExpire($format = 'Y-m-d H:i:s')
	{

		if ($this->session_expire === null || $this->session_expire === '') {
			return null;
		} elseif (!is_int($this->session_expire)) {
						$ts = strtotime($this->session_expire);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [session_expire] as date/time value: " . var_export($this->session_expire, true));
			}
		} else {
			$ts = $this->session_expire;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getCreatedDatetime($format = 'Y-m-d H:i:s')
	{

		if ($this->created_datetime === null || $this->created_datetime === '') {
			return null;
		} elseif (!is_int($this->created_datetime)) {
						$ts = strtotime($this->created_datetime);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [created_datetime] as date/time value: " . var_export($this->created_datetime, true));
			}
		} else {
			$ts = $this->created_datetime;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getUpdateDatetime($format = 'Y-m-d H:i:s')
	{

		if ($this->update_datetime === null || $this->update_datetime === '') {
			return null;
		} elseif (!is_int($this->update_datetime)) {
						$ts = strtotime($this->update_datetime);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [update_datetime] as date/time value: " . var_export($this->update_datetime, true));
			}
		} else {
			$ts = $this->update_datetime;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getEnabled()
	{

		return $this->enabled;
	}

	
	public function setId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = UserPeer::ID;
		}

	} 
	
	public function setUsername($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->username !== $v) {
			$this->username = $v;
			$this->modifiedColumns[] = UserPeer::USERNAME;
		}

	} 
	
	public function setPassword($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->password !== $v) {
			$this->password = $v;
			$this->modifiedColumns[] = UserPeer::PASSWORD;
		}

	} 
	
	public function setPhone($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->phone !== $v) {
			$this->phone = $v;
			$this->modifiedColumns[] = UserPeer::PHONE;
		}

	} 
	
	public function setGender($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->gender !== $v || $v === 0) {
			$this->gender = $v;
			$this->modifiedColumns[] = UserPeer::GENDER;
		}

	} 
	
	public function setAvatarPath($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->avatar_path !== $v) {
			$this->avatar_path = $v;
			$this->modifiedColumns[] = UserPeer::AVATAR_PATH;
		}

	} 
	
	public function setTotalRingtone($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->total_ringtone !== $v || $v === 0) {
			$this->total_ringtone = $v;
			$this->modifiedColumns[] = UserPeer::TOTAL_RINGTONE;
		}

	} 
	
	public function setPhoneModel($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->phone_model !== $v) {
			$this->phone_model = $v;
			$this->modifiedColumns[] = UserPeer::PHONE_MODEL;
		}

	} 
	
	public function setPhoneMaker($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->phone_maker !== $v) {
			$this->phone_maker = $v;
			$this->modifiedColumns[] = UserPeer::PHONE_MAKER;
		}

	} 
	
	public function setRingtoneId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->ringtone_id !== $v) {
			$this->ringtone_id = $v;
			$this->modifiedColumns[] = UserPeer::RINGTONE_ID;
		}

	} 
	
	public function setIsOnline($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->is_online !== $v || $v === 0) {
			$this->is_online = $v;
			$this->modifiedColumns[] = UserPeer::IS_ONLINE;
		}

	} 
	
	public function setSessionExpire($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [session_expire] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->session_expire !== $ts) {
			$this->session_expire = $ts;
			$this->modifiedColumns[] = UserPeer::SESSION_EXPIRE;
		}

	} 
	
	public function setCreatedDatetime($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [created_datetime] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->created_datetime !== $ts) {
			$this->created_datetime = $ts;
			$this->modifiedColumns[] = UserPeer::CREATED_DATETIME;
		}

	} 
	
	public function setUpdateDatetime($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [update_datetime] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->update_datetime !== $ts) {
			$this->update_datetime = $ts;
			$this->modifiedColumns[] = UserPeer::UPDATE_DATETIME;
		}

	} 
	
	public function setEnabled($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->enabled !== $v) {
			$this->enabled = $v;
			$this->modifiedColumns[] = UserPeer::ENABLED;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->username = $rs->getString($startcol + 1);

			$this->password = $rs->getString($startcol + 2);

			$this->phone = $rs->getString($startcol + 3);

			$this->gender = $rs->getInt($startcol + 4);

			$this->avatar_path = $rs->getString($startcol + 5);

			$this->total_ringtone = $rs->getInt($startcol + 6);

			$this->phone_model = $rs->getString($startcol + 7);

			$this->phone_maker = $rs->getString($startcol + 8);

			$this->ringtone_id = $rs->getInt($startcol + 9);

			$this->is_online = $rs->getInt($startcol + 10);

			$this->session_expire = $rs->getTimestamp($startcol + 11, null);

			$this->created_datetime = $rs->getTimestamp($startcol + 12, null);

			$this->update_datetime = $rs->getTimestamp($startcol + 13, null);

			$this->enabled = $rs->getInt($startcol + 14);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 15; 
		} catch (Exception $e) {
			throw new PropelException("Error populating User object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(UserPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			UserPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(UserPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = UserPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += UserPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collRingtones !== null) {
				foreach($this->collRingtones as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = UserPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collRingtones !== null) {
					foreach($this->collRingtones as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = UserPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getUsername();
				break;
			case 2:
				return $this->getPassword();
				break;
			case 3:
				return $this->getPhone();
				break;
			case 4:
				return $this->getGender();
				break;
			case 5:
				return $this->getAvatarPath();
				break;
			case 6:
				return $this->getTotalRingtone();
				break;
			case 7:
				return $this->getPhoneModel();
				break;
			case 8:
				return $this->getPhoneMaker();
				break;
			case 9:
				return $this->getRingtoneId();
				break;
			case 10:
				return $this->getIsOnline();
				break;
			case 11:
				return $this->getSessionExpire();
				break;
			case 12:
				return $this->getCreatedDatetime();
				break;
			case 13:
				return $this->getUpdateDatetime();
				break;
			case 14:
				return $this->getEnabled();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = UserPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getUsername(),
			$keys[2] => $this->getPassword(),
			$keys[3] => $this->getPhone(),
			$keys[4] => $this->getGender(),
			$keys[5] => $this->getAvatarPath(),
			$keys[6] => $this->getTotalRingtone(),
			$keys[7] => $this->getPhoneModel(),
			$keys[8] => $this->getPhoneMaker(),
			$keys[9] => $this->getRingtoneId(),
			$keys[10] => $this->getIsOnline(),
			$keys[11] => $this->getSessionExpire(),
			$keys[12] => $this->getCreatedDatetime(),
			$keys[13] => $this->getUpdateDatetime(),
			$keys[14] => $this->getEnabled(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = UserPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setUsername($value);
				break;
			case 2:
				$this->setPassword($value);
				break;
			case 3:
				$this->setPhone($value);
				break;
			case 4:
				$this->setGender($value);
				break;
			case 5:
				$this->setAvatarPath($value);
				break;
			case 6:
				$this->setTotalRingtone($value);
				break;
			case 7:
				$this->setPhoneModel($value);
				break;
			case 8:
				$this->setPhoneMaker($value);
				break;
			case 9:
				$this->setRingtoneId($value);
				break;
			case 10:
				$this->setIsOnline($value);
				break;
			case 11:
				$this->setSessionExpire($value);
				break;
			case 12:
				$this->setCreatedDatetime($value);
				break;
			case 13:
				$this->setUpdateDatetime($value);
				break;
			case 14:
				$this->setEnabled($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = UserPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setUsername($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setPassword($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setPhone($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setGender($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setAvatarPath($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setTotalRingtone($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setPhoneModel($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setPhoneMaker($arr[$keys[8]]);
		if (array_key_exists($keys[9], $arr)) $this->setRingtoneId($arr[$keys[9]]);
		if (array_key_exists($keys[10], $arr)) $this->setIsOnline($arr[$keys[10]]);
		if (array_key_exists($keys[11], $arr)) $this->setSessionExpire($arr[$keys[11]]);
		if (array_key_exists($keys[12], $arr)) $this->setCreatedDatetime($arr[$keys[12]]);
		if (array_key_exists($keys[13], $arr)) $this->setUpdateDatetime($arr[$keys[13]]);
		if (array_key_exists($keys[14], $arr)) $this->setEnabled($arr[$keys[14]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(UserPeer::DATABASE_NAME);

		if ($this->isColumnModified(UserPeer::ID)) $criteria->add(UserPeer::ID, $this->id);
		if ($this->isColumnModified(UserPeer::USERNAME)) $criteria->add(UserPeer::USERNAME, $this->username);
		if ($this->isColumnModified(UserPeer::PASSWORD)) $criteria->add(UserPeer::PASSWORD, $this->password);
		if ($this->isColumnModified(UserPeer::PHONE)) $criteria->add(UserPeer::PHONE, $this->phone);
		if ($this->isColumnModified(UserPeer::GENDER)) $criteria->add(UserPeer::GENDER, $this->gender);
		if ($this->isColumnModified(UserPeer::AVATAR_PATH)) $criteria->add(UserPeer::AVATAR_PATH, $this->avatar_path);
		if ($this->isColumnModified(UserPeer::TOTAL_RINGTONE)) $criteria->add(UserPeer::TOTAL_RINGTONE, $this->total_ringtone);
		if ($this->isColumnModified(UserPeer::PHONE_MODEL)) $criteria->add(UserPeer::PHONE_MODEL, $this->phone_model);
		if ($this->isColumnModified(UserPeer::PHONE_MAKER)) $criteria->add(UserPeer::PHONE_MAKER, $this->phone_maker);
		if ($this->isColumnModified(UserPeer::RINGTONE_ID)) $criteria->add(UserPeer::RINGTONE_ID, $this->ringtone_id);
		if ($this->isColumnModified(UserPeer::IS_ONLINE)) $criteria->add(UserPeer::IS_ONLINE, $this->is_online);
		if ($this->isColumnModified(UserPeer::SESSION_EXPIRE)) $criteria->add(UserPeer::SESSION_EXPIRE, $this->session_expire);
		if ($this->isColumnModified(UserPeer::CREATED_DATETIME)) $criteria->add(UserPeer::CREATED_DATETIME, $this->created_datetime);
		if ($this->isColumnModified(UserPeer::UPDATE_DATETIME)) $criteria->add(UserPeer::UPDATE_DATETIME, $this->update_datetime);
		if ($this->isColumnModified(UserPeer::ENABLED)) $criteria->add(UserPeer::ENABLED, $this->enabled);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(UserPeer::DATABASE_NAME);

		$criteria->add(UserPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setUsername($this->username);

		$copyObj->setPassword($this->password);

		$copyObj->setPhone($this->phone);

		$copyObj->setGender($this->gender);

		$copyObj->setAvatarPath($this->avatar_path);

		$copyObj->setTotalRingtone($this->total_ringtone);

		$copyObj->setPhoneModel($this->phone_model);

		$copyObj->setPhoneMaker($this->phone_maker);

		$copyObj->setRingtoneId($this->ringtone_id);

		$copyObj->setIsOnline($this->is_online);

		$copyObj->setSessionExpire($this->session_expire);

		$copyObj->setCreatedDatetime($this->created_datetime);

		$copyObj->setUpdateDatetime($this->update_datetime);

		$copyObj->setEnabled($this->enabled);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getRingtones() as $relObj) {
				$copyObj->addRingtone($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new UserPeer();
		}
		return self::$peer;
	}

	
	public function initRingtones()
	{
		if ($this->collRingtones === null) {
			$this->collRingtones = array();
		}
	}

	
	public function getRingtones($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseRingtonePeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collRingtones === null) {
			if ($this->isNew()) {
			   $this->collRingtones = array();
			} else {

				$criteria->add(RingtonePeer::CREATOR_ID, $this->getId());

				RingtonePeer::addSelectColumns($criteria);
				$this->collRingtones = RingtonePeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(RingtonePeer::CREATOR_ID, $this->getId());

				RingtonePeer::addSelectColumns($criteria);
				if (!isset($this->lastRingtoneCriteria) || !$this->lastRingtoneCriteria->equals($criteria)) {
					$this->collRingtones = RingtonePeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastRingtoneCriteria = $criteria;
		return $this->collRingtones;
	}

	
	public function countRingtones($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BaseRingtonePeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(RingtonePeer::CREATOR_ID, $this->getId());

		return RingtonePeer::doCount($criteria, $distinct, $con);
	}

	
	public function addRingtone(Ringtone $l)
	{
		$this->collRingtones[] = $l;
		$l->setUser($this);
	}


	
	public function getRingtonesJoinRingtoneCategory($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseRingtonePeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collRingtones === null) {
			if ($this->isNew()) {
				$this->collRingtones = array();
			} else {

				$criteria->add(RingtonePeer::CREATOR_ID, $this->getId());

				$this->collRingtones = RingtonePeer::doSelectJoinRingtoneCategory($criteria, $con);
			}
		} else {
									
			$criteria->add(RingtonePeer::CREATOR_ID, $this->getId());

			if (!isset($this->lastRingtoneCriteria) || !$this->lastRingtoneCriteria->equals($criteria)) {
				$this->collRingtones = RingtonePeer::doSelectJoinRingtoneCategory($criteria, $con);
			}
		}
		$this->lastRingtoneCriteria = $criteria;

		return $this->collRingtones;
	}

} 